/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.primefaces.model.map.LatLng;
import utility.ExceptionThrower;
import utility.ResourceBundleLoader;

/**
 *
 * @author Carmela
 */
public class SelezionaNegozio implements Serializable {

    private GestioneCliente cliente;
    private Map<Long, LatLng> mappaCordinate;

    public String getLatLen() {
        if (cliente != null) {
            if (cliente.getIdProvincia() != null) {
                LatLng tmp = mappaCordinate.get(cliente.getIdProvincia());
                return tmp.getLat() + "," + tmp.getLng();
            }
        }
        return "39.2953952, 16.2536091";
    }

    public GestioneCliente getCliente() {
        return cliente;
    }

    public void setCliente(GestioneCliente cliente) {
//        System.out.println("0");
//        if (cliente.getId() != null) {
//            if (this.provinciaComuni != null) {
//                Cliente c = cliente.getCliente();
//                if (c != null) {
//                    if (c.getComune() != null) {
//                        System.out.println("2");
//                        provinciaComuni.setProvincia(c.getComune().getProvincia());
//                        System.out.println("2");
//                        provinciaComuni.handleComuni();
//                        System.out.println("2");
//                        provinciaComuni.setComune(c.getComune());
//                        System.out.println("2");
//                        provinciaComuni.handleNegozi();
//                        System.out.println("2");
//                        this.eloggato = true;
//                    }
//                }
//            } else {
//                throw new IllegalStateException("Bisogna settare prima provincia comuni");
//            }
//        } if (cliente.getIdNegozio() != null) {
//            Negozio n = cliente.getNegozio();
//            if (n != null) {
//                if (n.getComune() != null) {
//                    if (n.getComune().getProvincia() != null) {
//                        System.out.println("1");
//                        provinciaComuni.setProvincia(n.getComune().getProvincia());
//                         System.out.println("1");
//                        provinciaComuni.handleComuni();
//                         System.out.println("1");
//                        provinciaComuni.setComune(n.getComune());
//                         System.out.println("1");
//                        provinciaComuni.handleNegozi();
//                         System.out.println("1");
//                        provinciaComuni.setNegozio(n);
//                         System.out.println("1");
//                    }
//                }
//            }
//        }
        this.cliente = cliente;
    }

    public void redirect() {
    }

    /**
     * Creates a new instance of SelezionaNegozio
     */
    public SelezionaNegozio() {
    }

    public String continua() {
        FacesContext fc = FacesContext.getCurrentInstance();
        ResourceBundle rb = ResourceBundleLoader.getResourceBundle(fc);
        if (!cliente.isNegozioValido()) {
            ExceptionThrower.throwMessageError(fc, rb, "negozioNonValidoErrorM");
            return "";
        }
        return "Spesa";
    }

    @PostConstruct
    public void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSession(true);
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);

        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);

        this.mappaCordinate = new HashMap<Long, LatLng>();
        this.mappaCordinate.put(1L, new LatLng(39.2953952, 16.2536091));
        this.mappaCordinate.put(2L, new LatLng(38.1092915, 15.6439327));
    }
}
